package com.bupt.liutong.admin.groupon.quartz;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;

import com.bupt.liutong.admin.core.base.CodeTableManager;
import com.bupt.liutong.admin.groupon.dao.GrouponDao;
import com.bupt.liutong.admin.groupon.dto.Groupon;
import com.bupt.liutong.admin.groupon.dto.Website;
import com.bupt.liutong.util.StringUtils;

/**
 * 
 * @author liutong628@gmail.com
 *
 */
public class GetCommonInfo implements GetGrouponInfo {

	@Autowired
	private GrouponDao grouponDao;

	public void execute(Website website) {
		SAXReader grouponReader = new SAXReader();
		Document grouponDoc;
		try {
			grouponDoc = grouponReader.read(new URL(website.getGrouponApi()));
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return;
		} catch (DocumentException e) {
			e.printStackTrace();
			return;
		}
		Element grouponRoot = grouponDoc.getRootElement();
		List<Element> grouponList = grouponRoot.selectNodes("//url");
		Groupon groupon = new Groupon();
		groupon.setWebsiteId(website.getId());

		CodeTableManager ctm = CodeTableManager.getInstance();
		Map<String, String> cityNameReverse = ctm.get("cityNameReverse");

		// 循环所有优惠券
		for (Element grouponElement : grouponList) {
			groupon.setLink(grouponElement.elementText("loc"));
			Node grouponNode = grouponElement.selectSingleNode("data/display");
			String cityId = cityNameReverse.get(grouponNode.selectSingleNode(
					"city").getText());
			if (StringUtils.isEmpty(cityId)) {
				// TODO log the city
				System.out.println(grouponNode.selectSingleNode("city")
						.getText());
				continue;
			} else
				groupon.setCityId(Integer.valueOf(cityId));
			groupon.setTitle(grouponNode.selectSingleNode("title").getText());
			groupon.setLargeImageUrl(grouponNode.selectSingleNode("image")
					.getText());
			groupon.setStartTime(new Date(Long.valueOf(grouponNode
					.selectSingleNode("startTime").getText()) * 1000L));
			groupon.setEndTime(new Date(Long.valueOf(grouponNode
					.selectSingleNode("endTime").getText()) * 1000L));
			groupon.setValue(Double.valueOf(grouponNode.selectSingleNode(
					"value").getText()));
			groupon.setPrice(Double.valueOf(grouponNode.selectSingleNode(
					"price").getText()));
			if (groupon.getPrice() == null)
				groupon.setRebate(null);
			else if (groupon.getPrice() == 0.0)
				groupon.setRebate(0.0);
			else if (groupon.getValue() == null)
				groupon.setRebate(null);
			else if (groupon.getValue() == 0.0)
				groupon.setRebate(0.0);
			else
				groupon.setRebate(groupon.getPrice() / groupon.getValue() * 10);
			groupon.setBought(Integer.valueOf(grouponNode.selectSingleNode(
					"bought").getText()));
			groupon.setCreateTime(website.getBatchTime());
			Groupon duplicate = grouponDao.getDupGroupon(groupon);
			//System.out.println(groupon.getStartTime());
			//System.out.println(groupon.getRebate());
			if (duplicate == null || duplicate.getId() == null)
				grouponDao.insertGroupon(groupon);
		}
	}

	public void setGrouponDao(GrouponDao grouponDao) {
		this.grouponDao = grouponDao;
	}

	public static void main(String[] args) {
	}
}
